package com.dreamscope.login.filter;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class ClickstreamFilter implements Filter {
	protected FilterConfig filterConfig;

	private final static String FILTER_APPLIED = "_clickstream_filter_applied";

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		System.out.println("In clickStream filter");

		// Ensure that filter is only applied once per request.
		if (request.getAttribute(FILTER_APPLIED) == null) {
			request.setAttribute(FILTER_APPLIED, Boolean.TRUE);

			HttpSession session = ((HttpServletRequest) request).getSession();
			Clickstream stream = (Clickstream) session.getAttribute("clickstream");
			stream.addRequest(((HttpServletRequest) request));
		}

		// pass the request on
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = filterConfig;
	}

	public void destroy() {
	}
}
